package com.goods.business.service.imp;

import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;

    @Override
    public List<ProductVO> findProductList(ProductVO productVo, String categorys) {
        if (!StringUtils.isEmpty(categorys)) {
            String[] split = categorys.split(",");
            switch (split.length) {
                case 3:
                    productVo.setThreeCategoryId(Long.parseLong(split[2]));
                case 2:
                    productVo.setTwoCategoryId(Long.parseLong(split[1]));
                case 1:
                    productVo.setOneCategoryId(Long.parseLong(split[0]));
                    break;
            }
        }

        Product product = new Product();
        BeanUtils.copyProperties(productVo, product);
        List<Product> select = productMapper.selectByProduct(product);
        List<ProductVO> collect = select.stream().map(product1 -> {
            ProductVO productVo2 = new ProductVO();
            BeanUtils.copyProperties(product1, productVo2);
            return productVo2;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Integer add(ProductVO productVO) {
        Date data = new Date();
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        product.setPNum(UUID.randomUUID().toString().replaceAll("-", ""));
//       product.setName((String) map.get("name"));
//       product.setImageUrl((String) map.get("imageUrl"));
//       product.setModel((String) map.get("model"));
//       product.setUnit((String) map.get("unit"));
//       product.setRemark((String) map.get("remark"));
        product.setCreateTime(data);
        product.setModifiedTime(data);
        product.setStatus(2);

        Long[] categoryKeys = productVO.getCategoryKeys();
        switch (categoryKeys.length) {
            case 3:
                product.setThreeCategoryId(categoryKeys[2]);
            case 2:
                product.setTwoCategoryId(categoryKeys[1]);
            case 1:
                product.setOneCategoryId(categoryKeys[0]);
                break;
        }
        return productMapper.insert(product);
    }

    @Override
    public ProductVO edit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product, productVO);
        return productVO;
    }

    @Override
    public Integer update(Long id, ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        Date date = new Date();
        product.setModifiedTime(date);
        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public Integer remove(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(1);
        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public Integer back(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(0);
        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public Integer delete(Long id) {

        return productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer publish(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(0);
        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public List<ProductVO> findProducts(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        List<Product> select = productMapper.selectByProduct(product);
        List<ProductVO> collect = select.stream().map(product1 -> {
            ProductVO productVO2 = new ProductVO();
            BeanUtils.copyProperties(product1, productVO2);
            return productVO2;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<ProductStockVO> findProductStocks(ProductVO productVO, String categorys) {
        if (!StringUtils.isEmpty(categorys)) {
            String[] split = categorys.split(",");
            switch (split.length) {
                case 3:
                    productVO.setThreeCategoryId(Long.parseLong(split[2]));
                case 2:
                    productVO.setTwoCategoryId(Long.parseLong(split[1]));
                case 1:
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    break;
            }
        }

        return productMapper.findProductStocks(productVO);
    }
}
